from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect
from django.views.decorators.csrf import csrf_protect
from django.template import RequestContext
from utils.urlresolvers import locreverse

from myapps.polls import models, forms


@csrf_protect
def create_f(request):
    return render_to_response('polls/create_f.html',
        {'form': forms.Create()},
        context_instance=RequestContext(request))


@csrf_protect
def create(request):
    form = forms.Create(request.POST)
    if form.is_valid():
        # Get data
        question = form.cleaned_data['question']
        choices = form.cleaned_data['choices']
        # Instantiate Poll
        poll = models.Poll(question=question)
        # Put Poll
        poll.put_with_choices(choices)
        poll.gput() #refresh poll count
        #poll.lput()
        return HttpResponseRedirect(locreverse('polls-index'))
    else:
        return render_to_response('polls/create_f.html',
            {'form': form},
            context_instance=RequestContext(request))